#!groovy
pipeline {
  agent any
  stages {
    stage('初始化') {
      steps {
        echo "【信息】打印环境变量初始值"
        sh "printenv"

        echo "【信息】Job全部参数：${params}"

        script {
          Common = load './common/common.groovy'
          Common.applicationRootAbsolute = Common.getApplicationRootAbsolute(params.APPLICATION_ROOT_DIR)
        }

        // 拉取部署文件
        dir(Common.pipelineCicdDir) {
          git env.GIT_URL
        }

        // 拉取应用代码，需要安装插件git parameter
        git branch: params.APPLICATION_GIT_BRANCH, url: params.APPLICATION_GIT_URL

        dir(Common.applicationRootAbsolute) {
          script {
            Common.cicdYamlData = Common.readCicdYaml(params.ENV_OPTION, Common.applicationRootAbsolute)
          }
        }
      }
    }
    stage('处理部署文件') {
      steps {
        dir(Common.applicationRootAbsolute) {
          script {
            if (Common.cicdYamlData.envData.k8sDeployFile) {
              // 使用自定义的k8s部署文件
              sh """sed -i "s,{{DOCKER_IMAGE}},${params.DOCKER_IMAGE},g" cicd/${Common.cicdYamlData.envData.k8sDeployFile}"""
            } else {
              // 使用默认模板
              sh """
                /bin/cp -rf ${Common.pipelineCicdDir}/template/java/k8s-deploy.yaml ./cicd/k8s-deploy.yaml
                sed -i "s,{{applicationName}},${Common.cicdYamlData.applicationName},g;\
                  s,{{port}},${Common.cicdYamlData.port},g;\
                  s,{{terminationGracePeriodSeconds}},${Common.cicdYamlData.envData.k8sDeployArgument.terminationGracePeriodSeconds ?: 70},g;\
                  s,{{requestsMemory}},${Common.cicdYamlData.envData.k8sDeployArgument.requestsMemory ?: "512Mi"},g;\
                  s,{{requestsCpu}},${Common.cicdYamlData.envData.k8sDeployArgument.requestsCpu ?: "500m"},g;\
                  s,{{limitsMemory}},${Common.cicdYamlData.envData.k8sDeployArgument.limitsMemory ?: "1Gi"},g;\
                  s,{{limitsCpu}},${Common.cicdYamlData.envData.k8sDeployArgument.limitsCpu ?: "1000m"},g;\
                  s,{{initialDelaySeconds}},${Common.cicdYamlData.envData.k8sDeployArgument.initialDelaySeconds ?: 60},g;\
                  s,{{jvmOptions}},${Common.cicdYamlData.envData.k8sDeployArgument.jvmOptions ?: "-XX:+UseZGC"},g;\
                  s,{{projectArguments}},${Common.cicdYamlData.envData.k8sDeployArgument.projectArguments},g;\
                  s,{{probePath}},${Common.cicdYamlData.envData.k8sDeployArgument.probePath},g;\
                  s,{{minReplicas}},${Common.cicdYamlData.envData.k8sDeployArgument.minReplicas ?: 1},g;\
                  s,{{maxReplicas}},${Common.cicdYamlData.envData.k8sDeployArgument.maxReplicas ?: 2},g;\
                  s,{{stabilizationWindowSeconds}},${Common.cicdYamlData.envData.k8sDeployArgument.stabilizationWindowSeconds ?: 60},g;\
                  s,{{DOCKER_IMAGE}},${params.DOCKER_IMAGE},g;" ./cicd/k8s-deploy.yaml
              """
            }
          }
        }
      }
    }
    stage("部署") {
      steps {
        dir(Common.applicationRootAbsolute) {
          script {
            def k8sDeployFile = Common.cicdYamlData.envData.k8sDeployFile
                ? Common.cicdYamlData.envData.k8sDeployFile : "k8s-deploy.yaml"
            println "【信息】k8sDeployFile是${k8sDeployFile}"

            for (int i = 0; i < Common.cicdYamlData.envData.ipArr.length; i++) {
              def serverIp = Common.cicdYamlData.envData.ipArr[i]
              // 部署
              sh """
                  ssh root@${serverIp} mkdir -p ~/${Common.cicdYamlData.applicationName};
                  scp cicd/${k8sDeployFile} root@${serverIp}:~/${Common.cicdYamlData.applicationName}/;
                  ssh root@${serverIp} kubectl apply -f ~/${Common.cicdYamlData.applicationName}/${k8sDeployFile};
                """
            }
          }
        }
      }
    }
  }
  post {
    success {
      script {
        if (Common.cicdYamlData.notice.wx.ciSuccessSend || Common.cicdYamlData.notice.wx.cdSuccessSend) {
          Common.curlWx(Common.cicdYamlData.notice.wx.key, env.JOB_NAME, Common.cicdYamlData.envData.envName, true)
        }
      }
    }
    failure {
      script {
        if (Common.cicdYamlData.notice.wx.ciFailureSend || Common.cicdYamlData.notice.wx.cdFailureSend) {
          Common.curlWx(Common.cicdYamlData.notice.wx.key, env.JOB_NAME, Common.cicdYamlData.envData.envName, false)
        }
      }
    }
  }
}